package com.liner.controller;

import com.liner.enums.ResponseEnum;
import com.liner.exception.ShopException;
import com.liner.result.Result;
import com.liner.service.SmsService;
import com.liner.util.RandomUtil;
import com.liner.utils.RegexValidateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/sms")
@Api(tags = "短信接口",description = "短信发送接口")
public class SmsController {

    @Autowired
    private SmsService smsService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("发送短信验证码")
    @ApiImplicitParam(name = "mobile",value = "手机号")
    @GetMapping("/send/{mobile}")
    public Result send(@PathVariable("mobile") String mobile){
        boolean b = RegexValidateUtil.checkMobile(mobile);
        if (!b){
            throw new ShopException(ResponseEnum.MOBILE_ERROR.getMsg());
        }
        String code = RandomUtil.getSixBitRandom();
        System.out.println(code);
        //把code传入 Redis

      /*  redisTemplate.opsForValue().set("LinShop-sms-code-"+mobile, code);
        return Result.ok();
*/

        boolean send = smsService.send(mobile, code);
        if (send) {
            //把code传入 Redis
            redisTemplate.opsForValue().set("LinShop-sms-code-"+mobile, code);
            return Result.ok();
        }
        return Result.fail();
    }

}
